package com.example.demo.demos.util;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.util.StatusPrinter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;

public class LogbackConfig {
    public LogbackConfig() {
    }

    public static final void init(LogConfigBean logConfigBean) {
        String env = logConfigBean.getEnv();
        String level = logConfigBean.getLevel();
        String path = logConfigBean.getPath();
        Integer maxHistory = logConfigBean.getMaxHistory();
        String maxFileSize = logConfigBean.getMaxFileSize();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<?xml version='1.0' encoding='UTF-8'?>\n");
        stringBuilder.append("<!DOCTYPE configuration>\n");
        stringBuilder.append("\n");
        stringBuilder.append("<configuration scan='true' scanPeriod='60 seconds' debug='false'>\n");
        stringBuilder.append("\n");
        stringBuilder.append("    <appender name='CONSOLE_DEBUG' class='ch.qos.logback.core.ConsoleAppender'>\n");
        stringBuilder.append("        <encoder>\n");
        stringBuilder.append("            <charset>UTF-8</charset>\n");
        stringBuilder.append("            <pattern>%boldWhite(%-5level #) %boldWhite([%date{yyyy-MM-dd HH:mm:ss:SSS}] #) %boldWhite(%thread #) %boldWhite(%logger) %boldWhite(%n%msg%n)</pattern>\n");
        stringBuilder.append("        </encoder>\n");
        stringBuilder.append("        <filter class='ch.qos.logback.classic.filter.LevelFilter'>\n");
        stringBuilder.append("            <level>DEBUG</level>\n");
        stringBuilder.append("            <onMatch>ACCEPT</onMatch>\n");
        stringBuilder.append("            <onMismatch>DENY</onMismatch>\n");
        stringBuilder.append("        </filter>\n");
        stringBuilder.append("    </appender>\n");
        stringBuilder.append("    <appender name='CONSOLE_INFO' class='ch.qos.logback.core.ConsoleAppender'>\n");
        stringBuilder.append("        <encoder>\n");
        stringBuilder.append("            <charset>UTF-8</charset>\n");
        stringBuilder.append("            <pattern>%boldGreen(%-5level #) %boldGreen([%date{yyyy-MM-dd HH:mm:ss:SSS}] #) %boldGreen(%thread #) %boldGreen(%logger) %boldGreen(%n%msg%n)</pattern>\n");
        stringBuilder.append("        </encoder>\n");
        stringBuilder.append("        <filter class='ch.qos.logback.classic.filter.LevelFilter'>\n");
        stringBuilder.append("            <level>INFO</level>\n");
        stringBuilder.append("            <onMatch>ACCEPT</onMatch>\n");
        stringBuilder.append("            <onMismatch>DENY</onMismatch>\n");
        stringBuilder.append("        </filter>\n");
        stringBuilder.append("    </appender>\n");
        stringBuilder.append("    <appender name='CONSOLE_WARN' class='ch.qos.logback.core.ConsoleAppender'>\n");
        stringBuilder.append("        <encoder>\n");
        stringBuilder.append("            <charset>UTF-8</charset>\n");
        stringBuilder.append("            <pattern>%boldYellow(%-5level #) %boldYellow([%date{yyyy-MM-dd HH:mm:ss:SSS}] #) %boldYellow(%thread #) %boldYellow(%logger) %boldYellow(%n%msg%n)</pattern>\n");
        stringBuilder.append("        </encoder>\n");
        stringBuilder.append("        <filter class='ch.qos.logback.classic.filter.LevelFilter'>\n");
        stringBuilder.append("            <level>WARN</level>\n");
        stringBuilder.append("            <onMatch>ACCEPT</onMatch>\n");
        stringBuilder.append("            <onMismatch>DENY</onMismatch>\n");
        stringBuilder.append("        </filter>\n");
        stringBuilder.append("    </appender>\n");
        stringBuilder.append("    <appender name='CONSOLE_ERROR' class='ch.qos.logback.core.ConsoleAppender'>\n");
        stringBuilder.append("        <encoder>\n");
        stringBuilder.append("            <charset>UTF-8</charset>\n");
        stringBuilder.append("            <pattern>%boldRed(%-5level #) %boldRed([%date{yyyy-MM-dd HH:mm:ss:SSS}] #) %boldRed(%thread #) %boldRed(%logger) %boldRed(%n%msg%n)</pattern>\n");
        stringBuilder.append("        </encoder>\n");
        stringBuilder.append("        <filter class='ch.qos.logback.classic.filter.LevelFilter'>\n");
        stringBuilder.append("            <level>ERROR</level>\n");
        stringBuilder.append("            <onMatch>ACCEPT</onMatch>\n");
        stringBuilder.append("            <onMismatch>DENY</onMismatch>\n");
        stringBuilder.append("        </filter>\n");
        stringBuilder.append("    </appender>\n");
        stringBuilder.append("\n");
        if ("online".equals(env)) {
            stringBuilder.append("    <appender name='FILE_DEBUG' class='ch.qos.logback.core.rolling.RollingFileAppender'>\n");
            stringBuilder.append("        <encoder>\n");
            stringBuilder.append("            <charset>UTF-8</charset>\n");
            stringBuilder.append("            <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss:SSS} %logger %n%msg%n%n</pattern>\n");
            stringBuilder.append("        </encoder>\n");
            stringBuilder.append("        <filter class='ch.qos.logback.classic.filter.LevelFilter'>\n");
            stringBuilder.append("            <level>DEBUG</level>\n");
            stringBuilder.append("            <onMatch>ACCEPT</onMatch>\n");
            stringBuilder.append("            <onMismatch>DENY</onMismatch>\n");
            stringBuilder.append("        </filter>\n");
            stringBuilder.append("        <rollingPolicy class='ch.qos.logback.core.rolling.TimeBasedRollingPolicy'>\n");
            stringBuilder.append("            <fileNamePattern>${log.file.path}/%d{yyyy-MM-dd}/1-debug#%d{yyyy-MM-dd}#%i.log</fileNamePattern>\n");
            stringBuilder.append("            <maxHistory>${log.file.maxHistory}</maxHistory>\n");
            stringBuilder.append("            <timeBasedFileNamingAndTriggeringPolicy class='ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP'>\n");
            stringBuilder.append("                <maxFileSize>${log.file.maxFileSize}</maxFileSize>\n");
            stringBuilder.append("            </timeBasedFileNamingAndTriggeringPolicy>\n");
            stringBuilder.append("        </rollingPolicy>\n");
            stringBuilder.append("    </appender>\n");
            stringBuilder.append("    <appender name='FILE_INFO' class='ch.qos.logback.core.rolling.RollingFileAppender'>\n");
            stringBuilder.append("        <encoder>\n");
            stringBuilder.append("            <charset>UTF-8</charset>\n");
            stringBuilder.append("            <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss:SSS} %logger %n%msg%n%n</pattern>\n");
            stringBuilder.append("        </encoder>\n");
            stringBuilder.append("        <filter class='ch.qos.logback.classic.filter.LevelFilter'>\n");
            stringBuilder.append("            <level>INFO</level>\n");
            stringBuilder.append("            <onMatch>ACCEPT</onMatch>\n");
            stringBuilder.append("            <onMismatch>DENY</onMismatch>\n");
            stringBuilder.append("        </filter>\n");
            stringBuilder.append("        <rollingPolicy class='ch.qos.logback.core.rolling.TimeBasedRollingPolicy'>\n");
            stringBuilder.append("            <fileNamePattern>${log.file.path}/%d{yyyy-MM-dd}/2-info#%d{yyyy-MM-dd}#%i.log</fileNamePattern>\n");
            stringBuilder.append("            <maxHistory>${log.file.maxHistory}</maxHistory>\n");
            stringBuilder.append("            <timeBasedFileNamingAndTriggeringPolicy class='ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP'>\n");
            stringBuilder.append("                <maxFileSize>${log.file.maxFileSize}</maxFileSize>\n");
            stringBuilder.append("            </timeBasedFileNamingAndTriggeringPolicy>\n");
            stringBuilder.append("        </rollingPolicy>\n");
            stringBuilder.append("    </appender>\n");
            stringBuilder.append("    <appender name='FILE_WARN' class='ch.qos.logback.core.rolling.RollingFileAppender'>\n");
            stringBuilder.append("        <encoder>\n");
            stringBuilder.append("            <charset>UTF-8</charset>\n");
            stringBuilder.append("            <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss:SSS} %logger %n%msg%n%n</pattern>\n");
            stringBuilder.append("        </encoder>\n");
            stringBuilder.append("        <filter class='ch.qos.logback.classic.filter.LevelFilter'>\n");
            stringBuilder.append("            <level>WARN</level>\n");
            stringBuilder.append("            <onMatch>ACCEPT</onMatch>\n");
            stringBuilder.append("            <onMismatch>DENY</onMismatch>\n");
            stringBuilder.append("        </filter>\n");
            stringBuilder.append("        <rollingPolicy class='ch.qos.logback.core.rolling.TimeBasedRollingPolicy'>\n");
            stringBuilder.append("            <fileNamePattern>${log.file.path}/%d{yyyy-MM-dd}/3-warn#%d{yyyy-MM-dd}#%i.log</fileNamePattern>\n");
            stringBuilder.append("            <maxHistory>${log.file.maxHistory}</maxHistory>\n");
            stringBuilder.append("            <timeBasedFileNamingAndTriggeringPolicy class='ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP'>\n");
            stringBuilder.append("                <maxFileSize>${log.file.maxFileSize}</maxFileSize>\n");
            stringBuilder.append("            </timeBasedFileNamingAndTriggeringPolicy>\n");
            stringBuilder.append("        </rollingPolicy>\n");
            stringBuilder.append("    </appender>\n");
            stringBuilder.append("    <appender name='FILE_ERROR' class='ch.qos.logback.core.rolling.RollingFileAppender'>\n");
            stringBuilder.append("        <encoder>\n");
            stringBuilder.append("            <charset>UTF-8</charset>\n");
            stringBuilder.append("            <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss:SSS} %logger %n%msg%n%n</pattern>\n");
            stringBuilder.append("        </encoder>\n");
            stringBuilder.append("        <filter class='ch.qos.logback.classic.filter.LevelFilter'>\n");
            stringBuilder.append("            <level>ERROR</level>\n");
            stringBuilder.append("            <onMatch>ACCEPT</onMatch>\n");
            stringBuilder.append("            <onMismatch>DENY</onMismatch>\n");
            stringBuilder.append("        </filter>\n");
            stringBuilder.append("        <rollingPolicy class='ch.qos.logback.core.rolling.TimeBasedRollingPolicy'>\n");
            stringBuilder.append("            <fileNamePattern>${log.file.path}/%d{yyyy-MM-dd}/4-error#%d{yyyy-MM-dd}#%i.log</fileNamePattern>\n");
            stringBuilder.append("            <maxHistory>${log.file.maxHistory}</maxHistory>\n");
            stringBuilder.append("            <timeBasedFileNamingAndTriggeringPolicy class='ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP'>\n");
            stringBuilder.append("                <maxFileSize>${log.file.maxFileSize}</maxFileSize>\n");
            stringBuilder.append("            </timeBasedFileNamingAndTriggeringPolicy>\n");
            stringBuilder.append("        </rollingPolicy>\n");
            stringBuilder.append("    </appender>\n");
            stringBuilder.append("\n");
        }

        stringBuilder.append("    <root>\n");
        stringBuilder.append("        <appender-ref ref='CONSOLE_DEBUG'/>\n");
        stringBuilder.append("        <appender-ref ref='CONSOLE_INFO'/>\n");
        stringBuilder.append("        <appender-ref ref='CONSOLE_WARN'/>\n");
        stringBuilder.append("        <appender-ref ref='CONSOLE_ERROR'/>\n");
        stringBuilder.append("        <appender-ref ref='FILE_DEBUG'/>\n");
        stringBuilder.append("        <appender-ref ref='FILE_INFO'/>\n");
        stringBuilder.append("        <appender-ref ref='FILE_WARN'/>\n");
        stringBuilder.append("        <appender-ref ref='FILE_ERROR'/>\n");
        stringBuilder.append("    </root>\n");
        stringBuilder.append("\n");
        stringBuilder.append("    <logger name='com.cdk' level='${log.level}'/>\n");
        stringBuilder.append("    <logger name='xc.crabapple' level='${log.level}'/>\n");
        stringBuilder.append("    <logger name='xc.begonia' level='${log.level}'/>\n");
        stringBuilder.append("    <logger name='com.alibaba' level='ERROR'/>\n");
        stringBuilder.append("    <logger name='com.gargoylesoftware' level='ERROR'/>\n");
        stringBuilder.append("    <logger name='org.hibernate' level='ERROR' />\n");
        stringBuilder.append("    <logger name='org.mybatis' level='ERROR'/>\n");
        stringBuilder.append("    <logger name='org.mongodb' level='ERROR'/>\n");
        stringBuilder.append("    <logger name='redis.clients' level='ERROR'/>\n");
        stringBuilder.append("    <logger name='org.activiti' level='ERROR' />\n");
        stringBuilder.append("    <logger name='org.apache' level='ERROR'/>\n");
        stringBuilder.append("    <logger name='org.springframework' level='ERROR'/>\n");
        stringBuilder.append("    <logger name='ch.qos.logback.core.joran' level='ERROR'/>\n");
        stringBuilder.append("\n");
        stringBuilder.append("</configuration>");
        String template = stringBuilder.toString();
        String url = path + "/logback.xml";
        String content = StringUtils.replaceAll(template, "${log.level}", level);
        content = StringUtils.replaceAll(content, "${log.file.path}", path);
        content = StringUtils.replaceAll(content, "${log.file.maxHistory}", maxHistory.toString());
        content = StringUtils.replaceAll(content, "${log.file.maxFileSize}", maxFileSize);
//        FileUtil.fileDelete(url);
//        FileUtil.fileWriteNew(url, content, true);
        configLoad(url);
    }

    public static void configLoad(String url) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);
        loggerContext.reset();

        try {
            configurator.doConfigure(url);
            StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
        } catch (Exception var4) {
            System.out.println("url=" + url);
            var4.printStackTrace();
        }

    }
}
